﻿using BCEntity.OA.PageBusiness.PageBusinessConfig;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
namespace BCData.OA.PageBusiness.PageBusinessConfig
{
    /// <summary>
    /// OA系统页面业务配置
    /// </summary>
	public class PageBusinessConfigData : IPageBusinessConfigData
    {
        private readonly IDatabaseContext databaseContext;
        private const string QUERY_EXISTS = @"SELECT 1 FROM OA_PageBusinessConfig";
        private const string QUERY = @"SELECT PageBusinessConfigId,PageBusinessCode,PageBusinessName,CreateTime,CompanyId,CompanyName FROM OA_PageBusinessConfig";
        public PageBusinessConfigData(IDatabaseContext databaseContext)
        {
            this.databaseContext=databaseContext;
        }

        public async Task<bool> Exists(long pageBusinessConfigId)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessConfigId=?PageBusinessConfigId", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<bool> Exists(string pageBusinessCode)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessCode=?PageBusinessCode", QUERY_EXISTS);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessCode", pageBusinessCode, DbType.AnsiString);
            return await this.databaseContext.Slave.ExecuteScalarAsync(sql, parameters) != null;
        }

        public async Task<PageBusinessConfigEntity> Add(PageBusinessConfigEntity entity)
        {
            string sql = SqlCommands.GetInsertSql("OA_PageBusinessConfig", new string[] { "PageBusinessCode", "PageBusinessName", "CompanyId","CompanyName" });
            sql = string.Format("{0} {1} WHERE PageBusinessConfigId=LAST_INSERT_ID();", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode, DbType.AnsiString);
            parameters.Add("?PageBusinessName", entity.PageBusinessName, DbType.AnsiString);
            parameters.Add("?CompanyId", entity.CompanyId, DbType.Int32,size:10);
            parameters.Add("?CompanyName", entity.CompanyName, DbType.AnsiString,size:50);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<PageBusinessConfigEntity>(sql, parameters, databaseContext.Transaction);

        }

        public async Task<PageBusinessConfigEntity> Update(PageBusinessConfigEntity entity)
        {
            string sql = SqlCommands.GetUpdateSql("OA_PageBusinessConfig", new string[] { "PageBusinessCode", "PageBusinessName" }, new string[] { "PageBusinessConfigId" });
            sql = string.Format("{0} {1} WHERE PageBusinessConfigId=?PageBusinessConfigId;", sql, QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", entity.PageBusinessConfigId, DbType.Int64);
            parameters.Add("?PageBusinessCode", entity.PageBusinessCode, DbType.AnsiString);
            parameters.Add("?PageBusinessName", entity.PageBusinessName, DbType.AnsiString);
            return await this.databaseContext.Master.QueryFirstOrDefaultAsync<PageBusinessConfigEntity>(sql, parameters, databaseContext.Transaction);
        }

        public async Task<bool> Delete(long pageBusinessConfigId)
        {
            string sql = @"DELETE FROM OA_PageBusinessConfig WHERE PageBusinessConfigId=?PageBusinessConfigId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
        }

        public async Task<PageBusinessConfigEntity> GetEntity(long pageBusinessConfigId)
        {
            string sql = string.Format(@"{0} WHERE PageBusinessConfigId=?PageBusinessConfigId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?PageBusinessConfigId", pageBusinessConfigId, DbType.Int64);
            return await this.databaseContext.Slave.QueryFirstOrDefaultAsync<PageBusinessConfigEntity>(sql, parameters);
        }

        public async Task<IEnumerable<PageBusinessConfigEntity>> GetList(int companyId)
        {
            string sql = string.Format("{0} WHERE CompanyId=?CompanyId", QUERY);
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?CompanyId", companyId, DbType.Int32);
            return await this.databaseContext.Slave.QueryAsync<PageBusinessConfigEntity>(sql, parameters);
        }

        public async Task<(IEnumerable<PageBusinessConfigEntity>, int)> Query(int companyId,string pageBusinessName, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            List<string> wheres = new List<string>();
            DynamicParameters parameters = new DynamicParameters();
            wheres.Add("CompanyId=?CompanyId");
            parameters.Add("?CompanyId", companyId, DbType.Int32);

            if (!string.IsNullOrWhiteSpace(pageBusinessName))
            {
                wheres.Add("PageBusinessName LIKE CONCAT('%',CONCAT(?PageBusinessName,'%'))");
                parameters.Add("?PageBusinessName", pageBusinessName, DbType.AnsiString);
            }
            if (start.HasValue && !end.HasValue)
            {
                wheres.Add("CreateTime>=?Start");
                parameters.Add("?Start", start.Value, DbType.DateTime);
            }
            else if (!start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime<=?End");
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            else if (start.HasValue && end.HasValue)
            {
                wheres.Add("CreateTime>=?Start AND CreateTime<=?End");
                parameters.Add("?Start", start.Value, DbType.DateTime);
                parameters.Add("?End", end.Value, DbType.DateTime);
            }
            string sql = string.Format(@"{0} {1} ORDER BY CreateTime DESC LIMIT {2},{3}; SELECT COUNT(1) FROM OA_PageBusinessConfig {1}",
                   QUERY,
                   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                   (pageIndex - 1) * pageSize,
                   pageSize);
            return await this.databaseContext.Slave.QueryPageableListAsync<PageBusinessConfigEntity>(sql, parameters);
        }
    }
}
